package com.jumpy.entities;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.jumpy.MovingRect;

public class Techo extends AnimatedSprite {

	public static final float DEMO_VELOCITY = 100.0f;
	private final PhysicsHandler mPhysicsHandler;

	public Techo(final float pX, final float pY,
			final TiledTextureRegion pTextureRegion,
			final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		this.setSize(256, 128);
		// this.setScale(2.0f, 1.0f);
		this.mPhysicsHandler = new PhysicsHandler(this);

		this.registerUpdateHandler(this.mPhysicsHandler);
		this.mPhysicsHandler.setVelocity(0.0f, Techo.DEMO_VELOCITY);
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		// if(this.mX < 0) {
		// this.mPhysicsHandler.setVelocityX(MovingRect.DEMO_VELOCITY);
		// } else if(this.mX + this.getWidth() > MovingRect.CAMERA_WIDTH) {
		// this.mPhysicsHandler.setVelocityX(-MovingRect.DEMO_VELOCITY);
		// }

		if (this.mY + this.getHeight() < 0) {
			this.mPhysicsHandler.setVelocityY(Techo.DEMO_VELOCITY);
		} else if (this.mY > MovingRect.CAMERA_HEIGHT) {
			this.setPosition(
					(int) ((MovingRect.CAMERA_WIDTH - this.getWidth()) * Math
							.random()), -this.getHeight());

		}

		super.onManagedUpdate(pSecondsElapsed);
	}
}